﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class WorkOrderTaskListControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void gvWorkOrderTasks_Add(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int WorkOrderid = hfWorkOrderId.Value == "" ? 0 : Convert.ToInt32(hfWorkOrderId.Value);
            if (companyid > 0 && WorkOrderid > 0)
                ucModalWorkOrderTaskControl.ShowModal(companyid, WorkOrderid, 0);
        }
        protected void gvWorkOrderTasks_Edit(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int taskid = Convert.ToInt32(e.CommandArgument.ToString());
            int WorkOrderid = hfWorkOrderId.Value == "" ? 0 : Convert.ToInt32(hfWorkOrderId.Value);
            if(WorkOrderid == 0)
                WorkOrderid = RSSBMCommonFunctions.GetWorkOrderIDByTaskID(companyid, taskid);

            if (companyid > 0 && WorkOrderid > 0 && taskid > 0)
                ucModalWorkOrderTaskControl.ShowModal(companyid, WorkOrderid, taskid);

        }
        protected void gvWorkOrderTasks_Delete(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int taskid = Convert.ToInt32(e.CommandArgument.ToString());
                int WorkOrderid = RSSBMCommonFunctions.GetWorkOrderIDByTaskID(companyid, taskid);
                if (companyid > 0 && WorkOrderid > 0 && taskid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderid", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@taskid", SqlDbType.Int, 0, ParameterDirection.Input, taskid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrderTask WHERE taskid=@taskid AND workorderid=@WorkOrderid AND companyid=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Task deleted successfully!", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        public void LoadWorkOrderTaskListByAll(int companyid)
        {
            SqlCommand myCommand;
            String sqlSELECT = "SELECT t.TaskId, t.TaskName, t.Description, t.Response, t.StartDT, t.EndDT, t.LaborCost, t.DiscountPercent, t.DiscountAmount, t.WorkOrderId, t.AssignTo, t.LocationId, t.StatusId, t.CompanyId, (select s.StatusName from rs_tblStatus s where s.StatusId=t.statusid) as Status FROM rs_tblWorkOrderTask t WHERE t.CompanyId=@CompanyId ORDER BY t.StartDT DESC";

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderTasks.DataSource = ds;
            gvWorkOrderTasks.DataBind();
            //lblCallCount.Text = gvWorkOrderTasks.Rows.Count.ToString();
            myConnection.Close();
        }
        public void LoadWorkOrderTaskListByID(int companyid, int WorkOrderid)
        {
            hfWorkOrderId.Value = WorkOrderid.ToString();
            SqlCommand myCommand;
            String sqlSELECT = "SELECT t.TaskId, t.TaskName, t.Description, t.Response, t.StartDT, t.EndDT, t.LaborCost, t.DiscountPercent, t.DiscountAmount, t.WorkOrderId, t.AssignTo, t.LocationId, t.StatusId, t.CompanyId, (select s.StatusName from rs_tblStatus s where s.StatusId=t.statusid) as Status FROM rs_tblWorkOrderTask t WHERE t.workorderid=@WorkOrderId AND t.CompanyId=@CompanyId ORDER BY t.StartDT DESC";

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvWorkOrderTasks.DataSource = ds;
            gvWorkOrderTasks.DataBind();
            //lblCallCount.Text = gvWorkOrderTasks.Rows.Count.ToString();
            myConnection.Close();
        }

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvWorkOrderTasks_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvWorkOrderTasks.PageIndex = e.NewPageIndex;
            gvWorkOrderTasks.DataBind();
        }

        protected void gvWorkOrderTasks_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvWorkOrderTasks.DataSource = dataView;
                gvWorkOrderTasks.DataBind();
            }
        }

    }

    
}